package com.concerneyes.glasses.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.glasses.model.GlassesItemModel;
import com.concerneyes.glasses.model.GlassesList;

import java.util.ArrayList;
import java.util.List;

import http.JsonMap;
import util.ImageUtil;

/**
 * Created by Administrator on 2017/6/19 0019.
 */

public class GlassesItemAdapter extends BaseAdapter {
    private Activity context;
    private List<GlassesList> data = new ArrayList<>();

    public GlassesItemAdapter(Activity context) {
        this.context = context;
    }

    public void clearList() {
        data.clear();
    }

    public void addDatas(GlassesItemModel datas) {
        if (datas != null) {
            this.data.addAll(datas.getList());
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public GlassesList getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_glasses, null);
            holder.glasses = (ImageView) convertView.findViewById(R.id.iv_item_glasses);
            holder.info = (TextView) convertView.findViewById(R.id.tv_glasses_title);
            holder.subtitle = (TextView) convertView.findViewById(R.id.subtitle);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.heightPixels;
        ViewGroup.LayoutParams lp = holder.glasses.getLayoutParams();
        lp.height = width / 5;
        holder.glasses.setLayoutParams(lp);

        String resLink = CEApp.getResLink(data.get(position).getCover());
        ImageUtil.glideloadImage(resLink, holder.glasses, R.drawable.ic_text_try);
        String title = data.get(position).getTitle();
        holder.info.setText(title);
        String subtitle = data.get(position).getSubtitle();
        holder.subtitle.setText(subtitle);
        return convertView;
    }

    class ViewHolder {
        ImageView glasses;
        TextView info;
        TextView subtitle;
    }
}
